package com.bio.action.user;

import java.util.Calendar;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.bio.action.AbstractAction;
import com.bio.entity.user.Company;
import com.bio.service.user.CompanyService;
import com.bio.utils.Environment;

@Namespace("/company")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/company/listCompanys.jsp", type = "dispatcher"),
		@Result(name = "toShowSelf", location = "company!showMyCompany", type = "redirect"),
		@Result(name = "tolist", location = "company!list", type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/company/editCompany.jsp", type = "dispatcher"),
		@Result(name = "editBySelf", location = "/WEB-INF/views/company/editCompanyBySelf.jsp", type = "dispatcher"),
		@Result(name = "showMyCompany", location = "/WEB-INF/views/company/showMyCompany.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/company/addCompany.jsp", type = "dispatcher")
		})
public class CompanyAction extends AbstractAction<Company, CompanyService> {

	Logger logger = Logger.getLogger(CompanyAction.class);
	
	private Company company;
	
	public CompanyAction(CompanyService service) {
		super(service);
	}

	private static final long serialVersionUID = -2403150075932963776L;

	public String list(){
		this.id = Environment.Session.getCompanyId();
		this.company = this.service.get(id);
		return "list";
	}
	
	public String add(){
		return "add";
	}
	
	public String save(){
		Calendar now = Calendar.getInstance();
		this.company.setCreatedDate(now);
		this.service.save(this.company);
		return "tolist";
	}
	
	public String update(){
		Company oldCompany = this.service.get(company.getId());
		this.company.setUpdatedDate(Calendar.getInstance());
		this.company.setCreatedDate(oldCompany.getCreatedDate());
		this.service.save(company);
		return "tolist";
	}
	
	public String edit(){
		this.company = this.service.get(id);
		return "edit";
	}
	
	public String delete(){
		this.service.remove(id);
		return "tolist";
	}
	
	public String showMyCompany(){
		this.id = Environment.Session.getCompanyId();
		this.company = this.service.get(id);
		return "showMyCompany";
	}
	
	public String editBySelf(){
		this.company = this.service.get(id);
		return "editBySelf";
	}
	
	public String updateBySelf(){
		Company oldCompany = this.service.get(company.getId());
		this.company.setUpdatedDate(Calendar.getInstance());
		this.company.setCreatedDate(oldCompany.getCreatedDate());
		this.service.save(company);
		return "toShowSelf";
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}
	
}
